import { columnsData, tableList } from './mockData'
import { ref, onMounted } from 'vue'
export function drillTableData () {
  const selectMenu = ref(['firstLevel'])
  const tableData = ref([])
  const tableColumns = ref([])
  const loading = ref(false)
  // 安装状态
  const installState = ref(true)
  // 提货状态
  const takeState = ref(true)
  // 订单状态
  const orderState = ref(true)
  // 获取数据，到时根据情况是否删除;从其它页面调用请求
  onMounted(() => {
    getDrillTableDetails()
  })
  async function getDrillTableDetails() {
    loading.value = true
    // 获取数据
    // const res = await getDrillTableData()
    // tableData.value = res
    tableData.value = tableList
    // 数据一进入根据用户选择是否显示自占比进行筛选
    tableColumns.value = isShowColumn(columnsData)
    loading.value = false
  }
  // 是否显示列表里面某列数据
  const isShowColumn = (tableColumns = []) => {
    return tableColumns
    .map(col => {
      // 深拷贝避免污染原始数据
      const newCol = {...col};
      
      if (newCol.children) {
        // 递归处理子列
        newCol.children = isShowColumn(newCol.children);
        // 保留有有效子列的父列
        return newCol.children.length > 0 ? newCol : null;
      }

      // 非自占比字段直接保留
      if (!newCol.prop?.includes('_self_rate')) return newCol;

      // 自占比字段根据状态判断
      const shouldShow = 
        (newCol.prop.startsWith('install_') && installState.value) ||
        (newCol.prop.startsWith('take_') && takeState.value) ||
        (newCol.prop.startsWith('order_') && orderState.value)

      return shouldShow ? newCol : null;
    })
    .filter(Boolean);
  }
  const handleState = (name) => {
    // 根据用户输入的参数，隐藏/显示对应的自占比
    const stateMap = {
      '安装': installState,
      '提货': takeState,
      '订单': orderState
    }
    if (stateMap[name]) {
      stateMap[name].value = !stateMap[name].value
      tableColumns.value = isShowColumn(columnsData)
    }
  }
  const mergeCells = ({ row, column, rowIndex }) =>{
    if (selectMenu.value.includes(column.property)) {
      return publicMergeCells(row, rowIndex, column.property, tableData.value)
    }
    return { rowspan: 1, colspan: 1 };
  }
  const setHeaderStyle = () => {
    return {
      color: "#1D2129",
      fontWeight: "500",
    }
  }

  const publicMergeCells = (row, rowIndex, attr, tableList) =>{
    if (!tableList || tableList.length === 0) {
      return { rowspan: 1, colspan: 1 }
    }
    if (rowIndex >= tableList.length) {
      return { rowspan: 1, colspan: 1 }
    }

    const currentValue = row[attr];
    // 明确处理 undefined、null 等情况，确保比较的准确性
    if (currentValue == null) { // 同时检查 undefined 和 null
      return { rowspan: 1, colspan: 1 };
    }
    const previousValue = rowIndex > 0 ? tableList[rowIndex - 1][attr] : null;
    if (currentValue === previousValue) {
      return { rowspan: 0, colspan: 0 };
    }

    let rowspan = 1
    for (let i = rowIndex + 1; i < tableList.length; i++) {
      // 使用严格相等比较，并处理可能为null或undefined的值
      if (tableList[i][attr] === currentValue) {
        rowspan++;
      } else {
        break;
      }
    }
    return { rowspan, colspan: 1 };
  }
  return {
    loading,
    tableData,
    tableColumns,
    installState,
    takeState,
    orderState,
    setHeaderStyle,
    mergeCells,
    handleState
  }
}